<template>
  <el-dialog v-model="dialogVisible" title="新增职业" width="30%" :before-close="handleClose" class="course-dialog-box">
    <el-form ref="jobFormRef" :model="ruleForm" :rules="rules" label-width="100px" size="large" status-icon>
      <el-form-item label="职业名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入名称" maxlength="30" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="职业描述" prop="description">
        <el-input
          type="textarea"
          v-model="ruleForm.description"
          placeholder="请输入描述"
          maxlength="200"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="职业薪酬">
        <div>
          <el-form-item>
            <el-input placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input placeholder="请输入"></el-input>
          </el-form-item>
        </div>
      </el-form-item>
      <el-form-item label="关联岗位信息" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入名称" maxlength="30" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="是否显示">
        <el-switch v-model="ruleForm.isShow"></el-switch>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm(courseFormRef)"> 保存 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, onBeforeMount, onMounted, inject, shallowRef, ref } from "vue";
import { ElMessageBox } from "element-plus";
let dialogVisible = inject("isShowAddDialog");
const handleClose = () => {
  // ElMessageBox.confirm("内容未保存，是否确认退出？", "提示").then(() => {
  dialogVisible.value = !dialogVisible.value;
  // });
};

// form表单
import type { FormInstance, FormRules } from "element-plus";
const courseFormRef = ref<FormInstance>();
const ruleForm = ref<Occupation.OccupationObj>({
  name: "",
  description: "",
  isShow: true,
});
const rules = reactive<FormRules<Course.CourseObj>>({
  name: [
    { required: true, message: "名称不能为空", trigger: "blur" },
    { min: 1, max: 30, message: "字数限制在1-30个字符", trigger: "blur" },
  ],
  description: [
    { required: true, message: "描述不能为空", trigger: "blur" },
    { min: 1, max: 30, message: "字数限制在1-200个字符", trigger: "blur" },
  ],
  subjectInfo: [
    {
      required: true,
      validator: (rule, value, callback) => {
        if (value === null || typeof value !== "object" || Object.keys(value).length === 0) {
          callback(new Error("请选择所属学科"));
        } else {
          callback(); // 校验通过
        }
      },
      trigger: "change",
    },
  ],
});
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      dialogVisible.value = false;
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>
<style lang="scss">
.course-dialog-box {
  min-width: 600px;
  .dialog-footer {
    button {
      padding: 15px 20px;
    }
  }
}
</style>
